package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo12 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = sc.nextLine();
        System.out.println("请输入密码:");
        String password = sc.nextLine();
        System.out.println("请输入昵称:");
        String nick = sc.nextLine();
        try(Connection conn = DBUtils.getConn()){
            //使用PreparedStatement 实现注册功能 并实现用户名不能重复的功能
            String sql1 = "select count(*) from user where username=?";
            PreparedStatement ps1 = conn.prepareStatement(sql1);
            ps1.setString(1,username);
            ResultSet rs = ps1.executeQuery();
            rs.next();
            int count =rs.getInt(1);
            if (count>0){
                System.out.println("用户名重复!");
                return;
            }
            String sql2 = "insert into user values(null,?,?,?)";
            PreparedStatement ps2 = conn.prepareStatement(sql2);
            ps2.setString(1,username);
            ps2.setString(2,password);
            ps2.setString(3,nick);
            ps2.executeUpdate();
            System.out.println("注册成功!");
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
    }
}
